<template>
  <van-cell-group class="item">
    <div class="header-date">
      <div>{{ bill.date }}</div>
      <div class="money">
        <span><b>支</b>{{ state.expense.toFixed(2) }}</span>
        <span><b>收</b>{{ state.income.toFixed(2) }}</span>
      </div>
    </div>
    <!--通过动态 class 的形式，判断是支出还是收入，分别给不同的类名-->
    <van-cell
      :class="{ expense: item.pay_type == 1, income: item.pay_type == 2 }"
      v-for="item in bill.bills"
      :key="item.id"
      :title="item.type_name"
      :value="`${item.pay_type == 1 ? '-' : '+'}${item.amount}`"
      @click="goToDetail(item)"
    />
  </van-cell-group>
</template>
<script setup lang="ts">
import { reactive,defineProps,defineExpose } from 'vue'
import { useRouter } from 'vue-router'

const props=defineProps<{bill:Array}>()

const router=useRouter();
const state = reactive({
      income: 0,
      expense: 0
    })

    state.income = props.bill.bills.filter(i => i.pay_type == 2).reduce((curr, item) => {
      curr += Number(item.amount)
      return curr
    }, 0)
    state.expense = props.bill.bills.filter(i => i.pay_type == 1).reduce((curr, item) => {
      curr += Number(item.amount)
      return curr
    }, 0)

    const goToDetail = (item:object) => {
      console.log(item.id)
      router.push({
        path: '/detail',
        query: {
          id: item.id
        }
      })
    }

    defineExpose({})
</script>


<style lang="scss" scoped>
  .item {
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0px 0px 4px 0px rgba(0, 0, 0, 0.1);
    margin-bottom: 10px;
    .header-date {
      height: 60px;
      display: flex;
      background-color: #f9f9f9;
      align-items: center;
      justify-content: space-between;
      padding: 0 10px;
      div {
        color: $color-text-base;
      }
      .money {
        span {
          margin-left: 20px;
          b {
            color: $color-text-caption;
            font-weight: normal;
            background-color: #f5f5f5;
            padding: 3px;
            margin-right: 2px;
          }
        }
      }
    }
  }
</style>
<style lang="scss">
  .expense {
    .van-cell__title {
      text-align: left;
    }
    .van-cell__value {
      color: $color-text-base!important;
    }
  }
  .income {
    .van-cell__title {
      text-align: left;
    }
    .van-cell__value {
      color: $text-warning!important;
    }
  }
</style>